﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using ProgramMain.Database;
using ProgramMain.Database.SimpleOpticNetDBTableAdapters;
using ProgramMain.Framework;

namespace ProgramMain.OpticNet
{
    public partial class FibreList : ProgramMain.Framework.BaseGridListCtl
    {
        public FibreList()
        {
            InitializeComponent();
            biDelete.Caption = "Разорвать";

            DevExpress.Utils.SuperToolTip superTool = new DevExpress.Utils.SuperToolTip();
            DevExpress.Utils.ToolTipTitleItem toolTipTitleItem5 = new DevExpress.Utils.ToolTipTitleItem();
            toolTipTitleItem5.Appearance.Image = global::ProgramMain.Properties.Resources.help_16;
            toolTipTitleItem5.Appearance.Options.UseImage = true;
            toolTipTitleItem5.Image = global::ProgramMain.Properties.Resources.help_16;
            toolTipTitleItem5.Text = "Разорвать";
            superTool.Items.Add(toolTipTitleItem5);
            this.biDelete.SuperTip = superTool;
        }

        private Nullable<Int32> _fibreID;
        /// <summary>
        /// Фильтр по объекту
        /// </summary>
        public Nullable<Int32> FibreID
        {
            get { return _fibreID; }
            set
            {
                if (_fibreID != value)
                {
                    _fibreID = value;
                    
                }
            }
        }

        protected override void LoadData()
        {
            //прочитать данные из базы и заполнить датасет
            this.fibreTableAdapter.SelectCommandTimeout = ProgramMain.Properties.Settings.Default.LongQueryTimeout;
            if (!DetailID.HasValue && !FibreID.HasValue)
            {
                simpleOpticNetDB.Fibre.Clear();
            }
            else
            {
                this.fibreTableAdapter.Fill(this.simpleOpticNetDB.Fibre, DetailID, null, FibreID, false, null);
            }

            UpdateStatusText(this.simpleOpticNetDB.Fibre);
        }

        protected override void UpdateData()
        {
            //внести изменения в базу
            this.fibreTableAdapter.SetUpdateTimeout = ProgramMain.Properties.Settings.Default.LongQueryTimeout;
            this.fibreTableAdapter.Update(this.simpleOpticNetDB.Fibre);
            UpdateStatusText(this.simpleOpticNetDB.OwnerCompanies);
        }

        override protected BaseModalDetailCtl CreateDetailDialog(BaseModalDetailCtl.ShowModeTypes pShowMode)
        {
            FibreDetail fibreDetail = new FibreDetail();
            fibreDetail.PresentInForm = this.Present;
            if (pShowMode == BaseModalDetailCtl.ShowModeTypes.Delete) fibreDetail.SaveCaption = "Разорвать";
            return fibreDetail;
        }
     
        public bool Equip2View
        {
            get { return colCurveElement2.Visible; }
            set { colCurveElement2.Visible = value; }
        }
    
        private void FibreList_DetailIDChanged(object sender, DetailEventArgs e)
        {
            DBRead();
            
        }

         protected override void Action_Delete(object sender, EventArgs e)
         {
             base.Action_Delete(sender, e);
             DBRead();
         }

         protected override void biDetails_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
         {
             if (Framework.BaseSecurity.CheckSecurityViewAction())
             {
                 FrmVertexes frmVertexes = FrmVertexes.ShowForm();
                 if (dataSource.Current != null)
                 {
                     SimpleOpticNetDB.FibreRow row =
                         (SimpleOpticNetDB.FibreRow)((DataRowView)dataSource.Current).Row;
                     frmVertexes.SetPath(row.ID);
                 }
             }
         }

        private void FibreList_Load(object sender, EventArgs e)
        {
            switch (Present)
            {
                case PresentFormType.InEquipment:
                    colCaption.Caption = "Порт";
                    colModul.Visible = false;
                    colColor.Visible = false;
                    colModul.VisibleIndex = -1;
                    colColor.VisibleIndex = -1;
                    colPortID.Visible = false;
                    colPortID.VisibleIndex = -1;
                    break;
                case PresentFormType.InCables:
                    colPortID.Visible = false;
                    colPortName.Visible = false;
                    colConnectedPortName.Visible = false;
                    break;
                case PresentFormType.InCableBoxes:
                    colPortID.Visible = false;
                    colColor.Visible = false;
                     colPortName.Visible = false;
                    colConnectedPortName.Visible = false;
                    break;
            }
        }

        protected override void biPath_ItemClick(object sender, ItemClickEventArgs e)
        {
            if(dataSource.Current ==null) return;
            SimpleOpticNetDB.FibreRow fibreRow = (SimpleOpticNetDB.FibreRow)((DataRowView)dataSource.Current).Row;
            FrmVertexes frmVertexes = FrmVertexes.ShowForm();
            frmVertexes.SetPath(fibreRow.ID);
        }

        protected override void biPathMap_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (dataSource.Current == null) return;
            SimpleOpticNetDB.FibreRow fibreRow = (SimpleOpticNetDB.FibreRow)((DataRowView)dataSource.Current).Row;
            FrmOpticMap map = FrmOpticMap.ShowForm();
            map.ClearLighted();
            VertexesTableAdapter vertexesTableAdapter = new VertexesTableAdapter();
            vertexesTableAdapter.ClearBeforeFill = true;
            vertexesTableAdapter.SelectCommandTimeout = Properties.Settings.Default.LongQueryTimeout;
            SimpleOpticNetDB.VertexesDataTable vertexesDataTable = vertexesTableAdapter.GetData(null, null, null, null,
                                                                                               null, null, fibreRow.ID, null, null);
            try
            {
                SimpleOpticNetDB.VertexesRow v = vertexesDataTable.Cast<SimpleOpticNetDB.VertexesRow>().First();
               
                map.SetCenterMapObject(v.Longitude, v.Latitude);
                foreach (SimpleOpticNetDB.VertexesRow item in vertexesDataTable.Cast<SimpleOpticNetDB.VertexesRow>())
                {
                    map.AddVertesLighted(item.ID);
                }
            }
            catch (Exception)
            {
            }

            CableTunnelsTableAdapter cableTunnelsTableAdapter = new CableTunnelsTableAdapter();
            cableTunnelsTableAdapter.ClearBeforeFill = true;
            cableTunnelsTableAdapter.SelectCommandTimeout = Properties.Settings.Default.LongQueryTimeout;
            SimpleOpticNetDB.CableTunnelsDataTable cableTunnelsDataTable = cableTunnelsTableAdapter.GetData(null, null, null, null, null, null, fibreRow.ID, null, null);

            foreach (SimpleOpticNetDB.CableTunnelsRow item in cableTunnelsDataTable)
            {
                map.AddCableTunnelLighted(item.ID);
            }
        }

       public void AutoEdit()
       {
           Action_Edit(biEdit, EventArgs.Empty);
       }
    }
}
